*------------------------------------------------------------------------------
* TESS 2010 data 
*==============================================================================
* recode some discrepancies
use "${dir_raw}/TESS2_023_Brashears_Client.dta",clear
keep CaseID A1*NAME 

local i = 1
foreach X of varlist A1*NAME {
	rename `X' name`i'
	local i = `i'+1
}

reshape long name@, i(CaseID) j(order)
replace name = strtrim(name)
drop if name == ""
outsheet * using "${dir_processed}/tess_2010_name.csv", comma replace 

insheet using "${dir_manual}/tess_2010_name_fix.csv", clear
keep caseid order n_name 
reshape wide n_name, i(caseid) j(order)
egen n_names = rowtotal(n_name*)
rename caseid CaseID 
save "${dir_processed}/tess_2010_name_fix.dta", replace 

* read data again 
use "${dir_raw}/TESS2_023_Brashears_Client.dta",clear
merge 1:1  CaseID using "${dir_processed}/tess_2010_name_fix.dta",gen(m_name)

gen id = CaseID 
gen wtall = weight 
gen exp_group = XTESS023 

gen r_age = PPAGE 
gen r_educ = PPEDUC 
gen r_sex = PPGENDER
	gen r_female = (PPGENDER ==2) if ~missing(PPGENDER)

recode PPETHM (1=1) (2=2) (3/5=3), gen(r_race)
	gen r_white = r_race == 1 if ~missing(r_race)
	gen r_black = r_race == 2 if ~missing(r_race)
	gen r_others = r_race == 3 if ~missing(r_race)

gen r_marstat = PPMARIT 
	gen r_married = PPMARIT == 1 if ~missing(PPMARIT) 
	gen r_single = PPMARIT == 5 if ~missing(PPMARIT) 

gen r_state = PPSTATEN 
gen r_wrkstat = PPWORK 
	recode r_wrkstat (1 2 =1) (else=0), gen(r_working)
	recode r_wrkstat (3 4=1) (else=0), gen(r_unemployed)
	recode r_wrkstat (5=1) (else=0), gen(r_retired)
	recode r_wrkstat (6 7=1) (else=0), gen(r_otherworks)


gen r_adults = PPHHSIZE 
gen r_partyid7 = 8 - XPARTY7
gen r_ideo = XIDEO if XIDEO > 0

gen r_no_religion = XREL1 == 13 if XREL1 > 0
gen r_attendance = XREL2  if XREL2  > 0
	replace r_attendance = 0 if r_no_religion == 1
*        50        -1  Refused
*       264         1  More than once a week
*       457         2  Once a week
*       166         3  Once or twice a month
*       327         4  A few times a year
*       269         5  Once a year or less
*       221         6  Never
*       307         .  

gen n_size = A1_COUNT 
gen n_why_noissue = A8 == 1 if A8 > 0 & ~missing(A8)

* corrected network size!! 
replace n_names = 0 if A1_COUNT == 0

* top-coding network size
recode n_names (6/max=6.5), gen(n_names6)
recode n_size (6/max=6.5), gen(n_size6)

* alter characteristics
gen a_type = A7 if A7 > 0

gen a_topic = A3 
gen a_name = NAME
gen a_name_sel = NAME_SEL // how many names were given 
gen a_name_order = NAME_NUM

gen a_material = A4 == 1 if A4 > 0 
gen a_social = A5 == 1 if A5 > 0
gen a_lend = A6 == 1 if A6 > 0

gen n_face = B1 if B1 >= 0
gen n_discuss = B2 if B2 >= 0
gen n_borrow = B3 if B3 >= 0
gen n_social = B4 if B4 >= 0

recode n_face (5000/max=5000)
recode n_discuss (500/max=500)
recode n_borrow (100/max=100)
recode n_social (500/max=500)

for var n_size n_names n_face n_discuss n_borrow n_social: gen lnX = ln(X+1)

gen year = 2010 
gen svydate2 = tm_start
format svydate2 %td

gen dataset = "TESS"
keep id wtall exp_group r_* year svydate2 dataset /*
*/ r_partyid n_size n_* a_* 

saveold "${dir_processed}/tess_2010.dta", replace version(12)

